// @ts-nocheck
import type { CustomParams } from '@/types/common'
import type { GetURLSearchParamsCallback } from '@/types/url'
import { formatParamsFromURL, formatParamsToURL } from '@/utils'


export const getURLSearchParams = (cb?: GetURLSearchParamsCallback): CustomParams => {
  const searchParams = new URLSearchParams(window.location.search)
  let res = Object.fromEntries(searchParams.entries())
  res = formatParamsFromURL(res)
  return cb?.(res) ?? res
}

export const getURLSearchString = (params: CustomParams) => {
  const res = formatParamsToURL(params)
  const searchParams = new URLSearchParams()
  for (const key in res) {
    const element = res[key]
    searchParams.append(key, element)
  }
  return `?${searchParams.toString()}`
}

export const getUrlParams = (handleURLParams?: GetURLSearchParamsCallback) => {
  let urlParams:CustomParams = {}
  let defaultParams:CustomParams = {}
  const _params = getURLSearchParams(handleURLParams)
  const isOpenDetail = _params.hasOwnProperty('drawerType') || window.location.hash === '#detail';
  if (isOpenDetail) {
    defaultParams = _params 
  } else {
    urlParams = _params
  }
  return {
    isOpenDetail,
    urlParams,
    defaultParams
  }
}

export const getMyUrlParams = () =>
  Object.fromEntries(new URLSearchParams(window.location.search).entries());